<template>
	<el-container style="height: 100%;overflow: hidden;">
		<!-- 头部区域 -->
		<el-header>
			<div>
				<!-- <img src="../assets/heima.png" alt=""> -->
				<span>电商后台管理系统</span>
			</div>
			<el-button type="info" @click="loginOut">退出</el-button>
		</el-header>
		<!-- 内容 -->
		<el-container>
			<!-- 侧边栏 -->
			<el-aside :width="isCollapse ? '64px' : '200px'">
				<div class="toggle-button" @click="toggleCollapse">|||</div>
				<!-- 侧边栏菜单区域 -->
				<el-menu :default-active="activePath" unique-opened router :collapse="isCollapse" :collapse-transition="false" 
					background-color="#333744" text-color="#fff" active-text-color="#409EFF">
					<!-- 一级菜单 -->
					<el-submenu :index="'/'+item.path" :key="item.id" v-for="item in menulist">
						<!-- 一级菜单模板区域 -->
						<template slot="title">
							<!-- 图标 -->
							<i :class="iconsObj[item.id]"></i>
							<!-- 文本 -->
							<span>{{ item.authName }}</span>
						</template>
						<el-menu-item :index="'/' + subItem.path" @click="saveNavStatus('/'+subItem.path)" :key="subItem.id" v-for="subItem in item.children">
							<template slot="title">
								<!-- 图标 -->
								<i class="el-icon-menu"></i>
								<!-- 文本 -->
								<span>{{ subItem.authName }}</span>
							</template>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<!-- 主体 -->
			<el-container style="height: 100%;overflow: hidden;">
				<!-- 内容 -->
				<el-main style="height: 100%;overflow: hidden;">
					<!-- 路由占位符，将子路径组件嵌套到这里 -->
					<router-view></router-view>
				</el-main>
			</el-container>
		</el-container>
	</el-container>
</template>

<script>
	export default {
		// 数据
		data() {
			return {
				// 左侧菜单数据
				menulist: [],
				// 定义每个菜单的图标类名
				iconsObj: {
					'125': 'iconfont icon-users',
					'103': 'iconfont icon-tijikongjian',
					'101': 'iconfont icon-shangpin',
					'102': 'iconfont icon-danju',
					'145': 'iconfont icon-baobiao'
				},
				// 是否折叠
				isCollapse: false,
				// 被激活的链接地址
				activePath: ''
			}
		},
		// 初始化加载
		created() {
			this.getMenuList();
			this.getActivePath();
		},
		methods: {
			// 退出方法
			loginOut() {
				// 首先清空token
				window.sessionStorage.clear();
				// 之后重新跳转至登录页
				this.$router.push("/login");
			},
			// 创建获取侧边栏菜单内容的方法
			async getMenuList() {
				const { data: result } = await this.$axios.get('menus');
				if (result.meta.status !== 200) return this.$message.error(result.meta.msg);
				this.menulist = result.data;
			},
			// 初始化获取存放的侧边栏状态（侧边栏记忆功能）
			getActivePath() {
				this.activePath = window.sessionStorage.getItem("navStatus");
			},
			// 侧边栏折叠方法
			toggleCollapse() {
				this.isCollapse = !this.isCollapse;
			},
			// 单击二级菜单保存当前菜单的 index
			saveNavStatus(status) {
				// 将当前选中的菜单的index保存到sessionStorage中
				window.sessionStorage.setItem("navStatus", status)
			}
		}
	};
</script>

<style lang="less" scoped>
	.el-container {
		height: 100%;
	}
	.el-menu {
		border: none;
	}
	.el-header {
		background-color: #373d41;
		display: flex;
		justify-content: space-between;
		padding-left: 5px;
		align-items: center;
		color: #FFF;
		font-size: 20px;

		>div {
			display: flex;
			align-items: center;

			span {
				margin-left: 20px;
				font-weight: 600;
				letter-spacing: 2px;
			}
		}
	}

	.el-aside {
		background-color: #333744;
	}

	.el-main {
		background-color: #EAEDF1;
	}

	.el-footer {
		background-color: #333744;
	}
	.iconfont {
		margin-right: 5px;
	}
	.toggle-button {
		background-color: #4A5064;
		opacity: .95;
		font-size:10px;
		line-height: 24px;
		color: #FFF;
		text-align: center;
		letter-spacing: 0.2em;
		cursor: pointer;
	}
	.toggle-button:hover {
		color: #409EFF;
		opacity: .8;
	}
</style>
